<?php
namespace app\admin\controller;

use app\common\model\OrderLogModel;
use app\common\controller\AdminController;
use think\App;
use app\common\model\OrderModel;
use app\common\model\CompanyModel;
use think\Db;
use think\exception\PDOException;

/**  YSF
 *   订单管理
 *   Date: 2019/1/3
 * Class Order
 * @package app\admin\controller
 */
class Order extends AdminController
{
    protected $orderModel;
    protected $companyModel;
    protected $orderLogModel;

    // 初始化构造方法
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->orderModel = new OrderModel();  // 订单模型
        $this->orderLogModel = new OrderLogModel(); // 订单日志模型
        $this->companyModel = new CompanyModel(); // 企业模型
    }

    // 订单列表
    public function index()
    {
        // ajax访问
        if ($this->request->get('type') == 'ajax') {
            // 接收参数
            $page = $this->request->get('page', 1);
            $limit = $this->request->get('limit', 10);
            $select = (array)$this->request->get('select', []);

            $where = [['is_del', '=', 1]]; // 未被删除
            // 搜索条件
            foreach ($select as $k => $v)
            {
                if ($k == 'order_sn' && $v != '') {
                    $where[] = [$k, 'LIKE', '%' . $v . '%'];
                }
            }
            $field = '*';
            $order = 'create_time DESC';
            // 列表查询
            $data = $this->orderModel->getAll($where, $field, $page, $limit,[], $order);
            // 数量查询
            $count = $this->orderModel->getCount($where);
            // 数据组装
            $info = [
                'limit'        => $limit,
                'page_current' => $page,
                'page_sum'     => ceil($count / $limit),
            ];
            $msg = '暂无数据！';
            if ($data) {
                $msg = '查询成功！';
                foreach ($data as $k => &$v)
                {
                    $v['company_id'] = $v['company_id'] ? $v['company_id'] : '暂无';
                    $v['order_type_name'] = empty($v['order_type'])?'':config('admin.order_type')[$v['order_type']];
                    $v['order_status_name'] = config('admin.order_status')[$v['order_status']];
                    $v['order_has_pay_money'] = '￥'.formatMoney($v['order_has_pay_money']);
                    $v['pay_way_name'] = empty($v['pay_way'])?'':config('admin.pay_way')[$v['pay_way']];
                    if ($v['pay_status'] == 1) {
                        $v['pay_status_name'] = '<span style="color: #ff0000">'.config('admin.pay_status')[$v['pay_status']].'</span>';
                    } else {
                        $v['pay_status_name'] = config('admin.pay_status')[$v['pay_status']];
                    }

                    $v['create_time'] = strlen($v['create_time']) > 5 ? dateTime($v['create_time']) : '';
                }
            }
            $list = [
                'code'  => 0,
                'msg'   => $msg,
                'count' => $count,
                'info'  => $info,
                'data'  => $data,
            ];
            return json($list);
        }

        // 基础数据
        $basic_data = [
            'title' => '管理员列表',
            'data'  => '',
        ];

        return $this->fetch('', $basic_data);
    }

    // 订单删除
    public function del()
    {
        // 接收参数
        $get = $this->request->get();

        $del = 0;
        // 批量删除操作
        if (is_array($get['id'])) {
            foreach ($get['id'] as $k => $v)
            {
                $where['order_id'] = $v;
                $data['is_del'] = 2;
                $del = $this->orderModel->edit($where, $data);
                unset($where);
            }
        }
        // 单条删除
        else {
            $where['order_id'] = $get['id'];
            $data['is_del'] = 2;
            $del = $this->orderModel->edit($where, $data);
            unset($where);
        }

        if ($del >= 1) {
            //清空菜单缓存
            clear_menu();
            return __success('删除成功！');
        } else {
            return __error('数据有误，请刷新重试！');
        }
    }

    // 订单确认
    public function confirmPay()
    {
        // 接收参数
        $order_id = $this->request->get('order_id') ?? '';
        if (empty($order_id)) {
            returnJson(1, '缺少参数');
        }

        $where[] = ['order_id', '=', $order_id];
        $where[] = ['pay_way', '=', 3];
        // 查询订单信息
        $order = $this->orderModel->getOne($where);
        if (empty($order)) {
            returnJson(1, '暂无订单数据');
        }

        // 开启事务
        Db::startTrans();
        try{
            // 1-- 修改订单状态
            $order_data['order_status'] = 2;
            $order_data['pay_time'] = time();
            $order_data['pay_status'] = 2;
            $this->orderModel->edit($where, $order_data);
            unset($where);
            // 2-- 添加订单日志
            $order_log['order_id'] = $order['order_id'];
            $order_log['operation'] = '支付成功';
            $order_log['operate_time'] = time();
            $order_log['remark'] = '对公后台确认支付';
            $this->orderLogModel->add($order_log);
            // 3-- 修改企业账户金额
            if ($order['order_type'] == 2) { // 2表示充值
                $where['company_id'] = $order['company_id'];
                $this->companyModel->setInc($where, 'current_money', $order['order_has_pay_money'] + $order['send_money']);
                $this->companyModel->setInc($where, 'cumulative_money', $order['order_has_pay_money'] + $order['send_money']);
                unset($where);
            }

            // 提交事务
            Db::commit();
            return __success('确认订单成功！');
        } catch (PDOException $e) {
            // 事务回滚
            Db::rollback();
            return __error('操作失败，请刷新重试！');
        }

    }








}